<?php

require_once 'Decorators.php';
require_once 'CoreForm.php';

class LocaleForm extends CoreForm {
	protected $countryElement = null;
	protected $stateElement = null;
	protected $cityElement = null;
	
	protected $languageElement = null;
	
	protected $page = 'registration';
	protected $addStar;
	
	/*
	 *  Add red star to the end of the element as indicator of the "required" field
	 */
	protected function addStarDecorator($element, $additionalDecorator = null, $addStar = true) {
		$decorators = $element->getDecorators ();
		
		$element->removeDecorator ( 'Errors' );
		$element->removeDecorator ( 'HtmlTag' );
		$element->removeDecorator ( 'Label' );
		
		if (is_object ( $additionalDecorator )) {
			$element->addDecorator ( $additionalDecorator );
		}
		
		if ($addStar) {
			if ($addStar === 2) {
				$element->addDecorator ( new StarDecoratorForState () );
			} else {
				if ($addStar === 3) {
					$element->addDecorator ( new StarDecoratorForStateInvisible () );
				} else {
					$element->addDecorator ( new StarDecorator () );
				}
			}
		}
		$element->addDecorators ( $decorators );
		
		return $element;
	}
	
	protected function addTopSpace($element, $pixels) {
		$sfd = new SupplementalFirstDecorator ();
		$sfd->Pixels = $pixels;
		$element->addDecorator ( $sfd );
		return $element;
	}
	
	function __construct($options = null, $translator = null, $addStar = true) {
		parent::__construct ( $options, $translator );
		$this->addStar = $addStar;
		
		$formId = isset ( $options ['form_id'] ) ? $options ['form_id'] : 'coreform';
		$this->setName ( $formId );
		
		$countryId = isset ( $options ['country_id'] ) ? ( int ) $options ['country_id'] : null;
		$stateId = isset ( $options ['state_id'] ) ? ( int ) $options ['state_id'] : null;
		$languageId = isset ( $options ['language_id'] ) ? ( int ) $options ['language_id'] : null;
		
		if (is_integer ( $countryId ) && $this->isCountry ( $countryId )) {
			
			$countryObj = $this->_api->CSCManager->GetCountry ( $countryId );
			$this->countryElement = $this->getCountryControl ( $countryId, $formId );
			if (is_integer ( $stateId ) && $this->isState ( $countryObj, $stateId )) {
				$stateObj = $countryObj->GetState ( $stateId );
				$this->stateElement = $this->getStateControl ( $countryObj, $stateId, $formId );
				$this->cityElement = $this->getCityControl ( $stateObj );
			} else {
				$this->stateElement = $this->getStateControl ( $countryObj, '', $formId );
				$this->cityElement = $this->getCityControl ( null, '' );
			}
		} else {
			$this->countryElement = $this->getCountryControl ( $this->_api->Country->Id, $formId );
			$this->stateElement = $this->getStateControl ( $this->_api->Country, '', $formId );
			$this->cityElement = $this->getCityControl ( null, '' );
		}
		
		if (is_integer ( $languageId ) && $this->isLanguage ( $languageId )) {
			$this->languageElement = $this->getLanguageControl ( $languageId );
		} else {
			$this->languageElement = $this->getLanguageControl ( $this->_api->Language->Id );
		}
	}
	
	private function getCountryControl($countryId, $formId) {
		// Create and configure country element:
		//echo $countryId;
		$countryCtrl = $this->createElement ( 'select', 'country_id', array ('class' => 'country' ) )->setLabel ( $this->translator->t ( 'Country', $this->page ) . ':' )->setRequired ( true )->setMultiOptions ( $this->getCountries () )->setAttrib ( "onchange", "updateState(this.value,'" . $formId . "')" )->setValue ( $countryId );
		$countryCtrl = $this->addStarDecorator ( $countryCtrl, null, $this->addStar );
		return $countryCtrl;
	}
	
	private function getLanguageControl($languageId) {
		$languageCtrl = $this->createElement ( 'select', 'language_id', array ('class' => 'language' ) )->setLabel ( $this->translator->t ( 'Language', $this->page ) . ':' )->setMultiOptions ( $this->getLanguages () )->setValue ( $languageId );
		return $languageCtrl;
	}
	
	private function getStateControl($country, $stateId, $formId) {
		$states = $country->GetStates ();
		if (count ( $states ) > 0) {
			$state = $this->createElement ( 'select', 'state_id', array ('class' => 'state' ) )->setMultiOptions ( $this->getRegions ( $country ) )->setAttrib ( "onchange", "updateCity(this.value,'" . $formId . "')" )->setValue ( $stateId )->setRequired ( true );
			$state->setLabel ( $this->translator->t ( 'Region/State', $this->page ) . ':' );
			$state = $this->addStarDecorator ( $state, null, 2 );
		} else {
			$state = $this->createElement ( 'text', 'state', array ('class' => 'state' ) );
			$state->setLabel ( $this->translator->t ( 'Region/State', $this->page ) . ':' );
			$state = $this->addStarDecorator ( $state, null, 3 );
		}
		return $state;
	}
	
	private function getCityControl($state, $cityId = 0) {
		
		$cities = array ();
		if (is_object ( $state )) {
			$cities = $this->getCities ( $state );
		}
		
		if (count ( $cities ) > 0) {
			$cityCtrl = $this->createElement ( 'select', 'city_id', array ('class' => 'city' ) )->setMultiOptions ( $cities )->setValue ( $cityId );
		} else {
			$cityCtrl = $this->createElement ( 'text', 'city', array ('class' => 'city' ) );
		}
		$cityCtrl->setLabel ( $this->translator->t ( 'City', $this->page ) . ':' )->setRequired ( true );
		$cityCtrl = $this->addStarDecorator ( $cityCtrl, null, $this->addStar );
		return $cityCtrl;
	}
	
	public function getCountries() {
		$countries = $this->_api->CSCManager->GetCountries ();
		$options = array ();
		foreach ( $countries as $country ) {
			$options [$country->Id] = $country->Name;
		}
		return $options;
	}
	
	private function getRegions($country) {
		$options = array ('' => $this->translator->t ( 'Select', 'common' ) );
		foreach ( $country->GetStates () as $state ) {
			$options [$state->Id] = $state->Name;
		}
		return $options;
	}
	
	private function getCities($state) {
		$cities = $state->GetCities ();
		if (count ( $cities ) > 0) {
			$options = array ('' => $this->translator->t ( 'Select', 'common' ) );
			foreach ( $state->GetCities () as $city ) {
				$options [$city->Id] = $city->Name;
			}
			return $options;
		}
		return null;
	}
	
	private function isCountry($id) {
		$countries = $this->_api->CSCManager->GetCountries ();
		foreach ( $countries as $country ) {
			if ($country->Id === $id) {
				return true;
			}
		}
		return false;
	}
	
	private function isState($country, $id) {
		$state = $country->GetState ( $id );
		if (is_object ( $state )) {
			return true;
		}
		return false;
	}
	
	public function getLanguages() {
		$languages = $this->_api->LanguageManager->GetLanguages ();
		$options = array ();
		foreach ( $languages as $language ) {
			$options [$language->Id] = $language->Name;
		}
		return $options;
	}
	
	private function isLanguage($id) {
		$languages = $this->_api->LanguageManager->GetLanguages ();
		foreach ( $languages as $language ) {
			if ($language->Id === $id) {
				return true;
			}
		}
		return false;
	}
	
	public function isValid($data) {
		$retval = parent::isValid ( $data );
		
		if (! $retval) {
			foreach ( $data as $key => $value ) {
				$element = $this->getElement ( $key );
				if (is_object ( $element )) {
					if ($element->hasErrors ()) {
						$element->setAttrib ( 'style', 'border: 1px solid #ff0000;' );
					}
				}
			}
		}
		
		return $retval;
	}

}

?>